function bvqxtools_update
% bvqxtools_update  - try autoupdate of toolbox
%
% FORMAT:       bvqxtools_update;
%
% No input/output fields.

% Version:  v0.7g
% Build:    9042319
% Date:     Apr-23 2009, 7:20 PM CEST
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% no argument check required.

% get current build
cb = bvqxtools_build;

% get last build available
if exist('bvqxtools_private', 'file') && ...
    bvqxtools_private
    lastbld = ['lastbuild_' bvqxtools_private(true) '.txt'];
else
    lastbld = 'lastbuild.txt';
end
lastinfo = splittocell( ...
    http_get(['http://bvqxtools.cjhammond.com/' lastbld]), ...
    char([13, 10]), 1, 1);
lb = str2double(regexprep(grep(lastinfo, 'Build'), ...
    '^.*Build\:\s*(\d+)\s*$', '$1'));

% compare builds
if cb < lb
    
    % show information
    disp([ ...
        'A new version is available:', char(10), ...
        char(10), ...
        gluetostring(strrep(lastinfo, '% ', ''), char(10)), char(10)]);
    
    % ask for download
    reply = input('Do you want to download the update? (y/n) ', 's');
    if ~strcmpi(reply, 'y')
        disp('Update aborted.');
        return;
    end
    
% no update available
else
    disp('You''re using the most recent version/build of BVQXtools.');
    return;
end

% update available, create temp folder
td = tempdir;
if td(end) == filesep
    td(end) = [];
end
td = sprintf('%s%cbvtu_%s', td, filesep, hxdouble(rand(1, 1)));
mkadir(td);
try
    cd(td);
    if ~strcmpi(td, pwd)
        error('NO_TEMP_DIR');
    end
catch
    error( ...
        'BVQXtools:FileError', ...
        'Error creating temp dir for update.' ...
    );
end

% for download, get filename and checksum
filename = regexprep(grep(lastinfo, 'Filename'), ...
    '^.*Filename\:\s+(.+)\s*$', '$1');
if iscell(filename)
    filename = filename{1};
end
filesize = str2double(regexprep(grep(lastinfo, 'Filesize'), ...
    '^.*Filesize\:\s*(\d+)\s*$', '$1'));
filechks = regexprep(grep(lastinfo, 'Checksum'), ...
    '^.*Checksum\:\s*([0-9a-fA-F]+)\s*$', '$1');
if iscell(filechks)
    filechks = filechks{1};
end

% download file
if filesize > 400000
    opt = {struct('chunk', 200000, 'progress', 1)};
else
    opt = {};
end
try
    http_getfile(['http://bvqxtools.cjhammond.com/' filename], filename, opt{:});
    thischks = md5sum([td filesep filename]);
    if ~strcmpi(thischks, filechks)
        error('CHECKSUM_ERROR');
    end
    filename = strrep(filename, '.mat', '.m');
    http_getfile(['http://bvqxtools.cjhammond.com/' filename], filename, opt{:});
    if ~isempty(checksyntax([td filesep filename]))
        try
            delete([td filesep strrep(filename, '.m', '.mat')]);
        catch
            % do nothing
        end
        error('SYNTAX_ERROR');
    end
catch
    try
        delete(filename);
    catch
        % do nothing
    end
    try
        cd('..');
        rmdir(td);
    catch
        % do nothing
    end
    error( ...
        'BVQXtools:InternalError', ...
        'Error downloading update. Please retry later...' ...
    );
end

% run update
rehash;
eval(strrep(filename, '.m', ''));
